<template>
    <MainSlot>
        <template v-slot:searchForm>
            <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag"
                @search="search" @isUnfold="(v) => { unfoldFlag = v }">
            </SearchForm>
        </template>
        <template v-slot:btn>
            <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
        </template>
        <template v-slot:table>
            <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :loading="loading"
                @selection-change="selectionChange">
                <template v-slot:whetherNormal="{ row }">
                    <span>{{ row.whetherNormal == 'true' ? '正常' : '异常' }}</span>
                </template>
                <template v-slot:remark="{ row }">
                    {{ row.remark || '--' }}
                </template>
                <template v-slot:imageList="{ row }">
                    <el-image style="width: 50px; height: 50px" :src="getImageList(row.pictureIdList)[0]"
                        :preview-src-list="getImageList(row.pictureIdList)"></el-image>
                </template>
            </TableData>
        </template>
        <template v-slot:pagination>
            <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
            </Pagination>
        </template>
    </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';
import TableData from '@/components-production-management/TableData';
import EnumSlot from '@/components-production-management/EnumSlot';

//批次
import batchManagement from '@/api/production-management/batch-management.js';
//鱼塘
import fishPondApi from "@/api/basic-info-manage/fishPond";
//电子巡塘接口
import inspectionElectronicPatrolApi from "@/api/inspection-management/inspection-electronicPatrol"
//品种
import fishVarietyApi from "@/api/basic-info-manage/fishVariety"
//规格
import specificationApi from "@/api/basic-info-manage/specification/index";
//获取员工
import staffApi from "@/api/basic-info-manage/staff";
export default {
    components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, EnumSlot },
    data() {
        return {
            unfoldFlag: false,
            loading: false,
            dialogVisible: false,
            dialogVisible_lookView: false,
            selectList: [],
            unitList: [],
            formData: {},
            page: 1,
            total: 10,
            selectIndex: 0,
            searchFormData: {},
            btnList: ['delete'],
            dataList: [],
            tableThead: [
                { isShow: true, label: '巡塘人', prop: 'staffName' },
                { isShow: true, label: '是否异常', slotName: 'whetherNormal' },
                { isShow: true, label: '巡检说明', slotName: 'remark' },
                { isShow: true, label: '巡塘图片', slotName: 'imageList' },
                { isShow: true, label: '电子巡塘时间', prop: 'inspectionTime' }
            ],
            // 配置项
            table_config: {
                isCheckBox: true,

                isOperation: true,
                // 表格操作列
                operation: {
                    label: '操作',
                    width: '200',
                    cols: [
                        {
                            label: '删除',
                            type: 'danger',
                            flag: 'del',
                            handleRow: this.handleRow
                        }
                    ]
                },
            },
            formItemList: [
                {
                    label: '巡塘人',
                    type: 'select',
                    selectOptions: [],
                    param: 'staffName',
                    defaultSelect: '', // 下拉框默认选中项
                    multiple: false,
                    isShow: true
                },
                {
                    label: '巡塘日期',
                    type: 'datetimerange',
                    param: 'dateTime',
                    multiple: false,
                    isShow: true
                },
            ],
            fishPondList: [],
            batchList: [],
            allbatchList: [],
            staffList: [],
            specificationList: [],
            fishVarietyList: [],
            breedTypeList: [],
            breedTypeObj: {}
        }
    },
    mounted() {
        this.queryPageByCondition();
        this.initData();
    },
    methods: {
        //初始化数据
        async initData() {
            let { data: staffList } = await staffApi.queryByCondition()
            this.staffList = staffList || [];
            this.setFormOptions();
        },
        setFormOptions() {
            let list = JSON.parse(JSON.stringify(this.formItemList));
            this.formItemList = []
            list.forEach((item, index) => {
                if (item.param.indexOf('staffName') !== -1) {
                    let selectOptions = this.staffList.map(data => {
                        return {
                            text: data.staffName, value: data.staffName
                        }
                    })
                    item.defaultSelect = this.searchFormData[item.param];
                    item.selectOptions = selectOptions;
                };

            });
            this.$nextTick(() => {
                this.$set(this, 'formItemList', list)
                console.log(this.formItemList)
            })
        },
        getImageList(pictureIdList = []) {
            let pictureList = pictureIdList.map(id => {
                return `${this.$baseurl.fileServiceUrl}base/files/preview?fileId=${id}`
            })
            return pictureList
        },
        //批量删除
        delList(deleteList = [], key = 'electronicPatrolId') {
            let list = []
            deleteList.forEach(item => {
                list.push(item[key])
            })
            console.log(list)
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let res = await inspectionElectronicPatrolApi.delListById({ idList: list });
                if (res.success) {
                    this.$message.success(res.message || '删除成功');
                    this.queryPageByCondition();
                } else {
                    this.$message.warning(res.message || '删除失败');
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //查询
        queryPageByCondition() {
            let params = {
                page: this.page,
                size: 10,
                entity: {
                    ...this.searchFormData
                }
            }
            this.loading = true;
            inspectionElectronicPatrolApi.queryPageByCondition(params).then(res => {
                this.loading = false;
                if (res.success == 1) {
                    this.dataList = res.data.content || [];
                    this.total = res.data.totalElements;
                    if (res.data.totalPages && this.page > res.data.totalPages) {
                        this.page = res.data.totalPages;
                        this.queryPageByCondition();
                    }
                } else {
                    this.$message.warning(res.message || '分页查询失败')
                }
            }).catch(err => {
                this.loading = false;
                console.log(err)
            })
        },
        //搜索
        search(data) {
            this.page = 1;
            let endData = null;
            let startData = null;
            if (data.dateTime && data.dateTime.length == 2) {
                startData = `${data.dateTime[0]} 00:00:00`;
                endData = `${data.dateTime[1]} 23:59:59`;
            }
            this.searchFormData = {
                ...this.searchFormData,
                ...data,
                endTime: endData,
                startTime: startData
            };
            this.queryPageByCondition();
        },
        //翻页查询
        handleCurrentChange(page) {
            this.page = page;
            this.queryPageByCondition();
        },
        //查看详情
        lookView(row) {
            console.log(row)
            this.dialogVisible_lookView = true;
        },
        // table操作方法
        handleRow(rowData, item) {
            if (item.flag == 'del') {
                this.delList([rowData]);
            }
        },
        // 多选方法
        selectionChange(val) {
            this.selectList = val || []
        },
        //获取选中Btn
        getBtnEvent(flag) {
            if (flag == 'delete') {
                if (this.selectList.length > 0) {
                    this.delList(this.selectList)
                } else {
                    this.$message.warning('请先选择表单数据项后再删除！')
                }
            }
        },
        //关闭弹窗
        closeDialog() {
            this.dialogVisible = false;
            this.queryPageByCondition();
        },
    }
};
</script>
<style lang="scss" scoped></style>